package com.sls.web.manager.sys.service.impl;

import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;
import com.sls.web.manager.sys.pojo.Permission;
import com.sls.web.manager.sys.querybean.PermissionQuery;
import com.sls.web.manager.sys.service.PermissionService;

@Service("permissionService")
public class PermissionServiceImpl extends SuperService implements
		PermissionService {
	private static Logger log = Logger.getLogger(PermissionServiceImpl.class);
	private Gson gson = null;

	public PermissionServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean add(Permission permission, UserInfoVO userInfoVO)
			throws Exception {		
		String ip = userInfoVO.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.ADD_MENU);
		head.setUserId(userInfoVO.getUserId());
		if (permission.getLevel() == 0) {
			if (permission.getIsMenu())
				permission.setLevel(1);
		} else {
			permission.setIsMenu(true);
		}
		SocketDTO dto = super.send(head, permission);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

	@Override
	public List<Permission> queryAllInfo(UserInfoVO userInfoVO)
			throws Exception {
		log.info("Enter PermissionService queryAllInfo method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.DELETE_MENUBY_CODE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		String body = dto.getBody();
		List<Permission> permissionList = gson.fromJson(body,
				new TypeToken<List<Permission>>() {
				}.getType());
		return permissionList;
	}

	@Override
	public Permission queryInfoByServiceCode(String serviceCode,
			UserInfoVO userInfoVO) throws Exception {
		log.info("Enter PermissionService queryInfoByServiceCode method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_MENU_BY_CODE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		Permission p = new Permission();
		p.setCode(serviceCode);
		SocketDTO dto = super.send(head, p);
		Permission permission = gson.fromJson(dto.getBody(), Permission.class);
		return permission;
	}

	@Override
	public boolean update(Permission permission, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.UPDATE_MENU);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		if (permission.getLevel() == 0) {
			if (permission.getIsMenu())
				permission.setLevel(1);
		}
		SocketDTO dto = super.send(head, permission);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

	@Override
	public Pagination<Permission> queryPermission(PermissionQuery pQuery,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_MENU);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, pQuery);
		Pagination<Permission> permission = gson.fromJson(dto.getBody(),
				new TypeToken<Pagination<Permission>>() {
				}.getType());
		return permission;
	}

	@Override
	public void delete(String serviceCode, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.DELETE_MENU);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		Permission p = new Permission();
		p.setCode(serviceCode);
		SocketDTO dto = super.send(head, p);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode()))
			return;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());

	}
}
